package com.hl.sfzy.service.content.impl;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.content.Article;
import com.hl.sfzy.mapper.content.article.ArticleMapper;
import com.hl.sfzy.service.content.ArticleService;

/**
 * @description: 文章服务实现类
 * @version 0.1.0
 * @company : www.hailiang.tech
 * @authour : fireflyshen
 */
@Service
public class ArticleServiceImpl implements ArticleService{

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public  PageInfo<Article> getArticles(Integer paegNum,Integer pageSize) {
        PageHelper.startPage(paegNum,pageSize);
        List<Article> byAll = articleMapper.findByAll();
        List<Article> orElse = Optional.ofNullable(byAll).orElse(Collections.emptyList());
        PageInfo<Article> pageInfo = new PageInfo<>(orElse);
        return pageInfo;
    }

    @Override
    public int addArticle(Article article) {
        return articleMapper.saveArticle(article);
    }

    @Override
    public int removeArticle(Integer id) {
        return articleMapper.deleteArticle(id);
    }

    @Override
    public Article getArticleById(Long id) {
       return articleMapper.findArticleById(id);
    }

    @Override
    public  PageInfo<Article> searchArticles(String key,LocalDateTime starTime,LocalDateTime endTime,Integer paegNum,Integer pageSize) {
        PageHelper.startPage(paegNum,pageSize);
        List<Article> articles = articleMapper.searchArticles(key, starTime, endTime);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return  pageInfo;
    }

    @Override
    public int updateArticle(Article article) {
        return articleMapper.updateArticle(article);
    }


    
     
}
